package com.tencent.sr.iris.basic.mp.restapi.controller.task;

import com.tencent.sr.iris.activity.interfaces.client.task.GroupTaskCoreClient;
import com.tencent.sr.iris.activity.interfaces.client.task.IrisGroupTaskClient;
import com.tencent.sr.iris.activity.interfaces.request.task.*;
import com.tencent.sr.iris.activity.interfaces.response.activity.ActivityAwardInfoResponse;
import com.tencent.sr.iris.activity.interfaces.response.group.task.*;
import com.tencent.sr.iris.basic.mp.interfaces.request.MpGetActivityConfigAwardRequest;
import com.tencent.sr.iris.basic.mp.service.activity.ActivityConfigService;
import com.tencent.sr.iris.basic.mp.service.task.GroupTaskService;
import com.tencent.sr.iris.basic.service.interfaces.client.ActivityMessageClient;
import com.tencent.sr.iris.basic.service.interfaces.request.ActivitySubscribeRequest;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.springbootstarter.authtoken.localbean.AppUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Api(tags = "团长任务", value = "团长任务相关接口")
@RestController
@RequestMapping(value = {"/iris/activity/mp/v1/group/task"})
public class GroupTaskController {

    @Resource
    private AppUser appUser;
    @Resource
    private IrisGroupTaskClient groupTaskClient;
    @Resource
    private GroupTaskService groupTaskService;
    @Resource
    private ActivityMessageClient activityMessageClient;
    @Resource
    private GroupTaskCoreClient groupTaskCoreClient;
    @Resource
    private ActivityConfigService activityConfigService;

    @PostMapping("getConfigAwardInfo")
    @ApiOperation("查询活动奖励配置金额")
    public HttpResult<ActivityAwardInfoResponse> getConfigAwardInfo(
            @Validated @RequestBody MpGetActivityConfigAwardRequest request) {
        ActivityAwardInfoResponse result = activityConfigService.getConfigAwardInfo(request);
        return HttpResult.succ(result);
    }

    @PostMapping("/popCallBack")
    @ApiOperation(value = "首页弹窗成功回调")
    public HttpResult<Boolean> popCallBack() {
        return HttpResult.succ(groupTaskService.popCallBack(appUser.getUid()));
    }

    @PostMapping("/needPop")
    @ApiOperation(value = "首页是否需要弹窗")
    public HttpResult<Boolean> needPop() {
        return HttpResult.succ(groupTaskService.needPop(appUser.getUid()));
    }

    @PostMapping("/pop")
    @ApiOperation(value = "首页弹窗")
    public HttpResult<GroupTaskPopResponse> pop() {
        GroupTaskPopRequest request = new GroupTaskPopRequest();
        request.setUnionId(appUser.getWxUnionId());
        return groupTaskClient.pop(request);
    }

    @PostMapping("/config")
    @ApiOperation(value = "查询团长任务配置")
    public HttpResult<GroupTaskConfigResponse> config() {
        return groupTaskClient.config();
    }

    @PostMapping("/subscribe")
    @ApiOperation(value = "团长任务消息订阅")
    public HttpResult<Boolean> subscribe(@RequestBody @Validated ActivitySubscribeRequest request) {
        request.setUid(appUser.getUid());
        return activityMessageClient.subscribe(request);
    }

    @PostMapping("/reward")
    @ApiOperation(value = "查询团长任务奖励金")
    public HttpResult<GroupTaskRewardResponse> taskReward(@RequestBody @Validated GroupTaskRewardRequest request) {
        request.setUnionId(Objects.isNull(request.getUnionId()) ? appUser.getWxUnionId()
                : request.getUnionId());
        return groupTaskClient.taskReward(request);
    }

    @PostMapping("/pageRewardList")
    @ApiOperation(value = "分页查团长任务奖励详情")
    HttpResult<PaginationResponse<GroupTaskRewardDetailResponse>> pageRewardList(
            @RequestBody @Validated PaginationRequest<GroupTaskRewardDetailRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskRewardDetailRequest());
        }
        request.getQueryCriteria().setUnionId(
                Objects.isNull(request.getQueryCriteria().getUnionId()) ? appUser.getWxUnionId()
                        : request.getQueryCriteria().getUnionId());
        return groupTaskClient.pageRewardList(request);
    }

    @PostMapping("/list/inProgress")
    @ApiOperation(value = "查询团长任务列表-进行中")
    HttpResult<PaginationResponse<GroupTaskListResponse>> listInProgress(
            @RequestBody @Validated PaginationRequest<GroupTaskListNewRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskListNewRequest());
        }
        request.getQueryCriteria().setUnionId(appUser.getWxUnionId());
        request.getQueryCriteria().setUid(appUser.getUid());
        PaginationResponse<GroupTaskListResponse> response = groupTaskService.listInProgress(
                request);
        return HttpResult.succ(response);
    }

    @PostMapping("/list/ended")
    @ApiOperation(value = "查询团长任务列表-已结束")
    HttpResult<PaginationResponse<GroupTaskListResponse>> listEnded(
            @RequestBody @Validated PaginationRequest<GroupTaskListNewRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new GroupTaskListNewRequest());
        }
        request.getQueryCriteria().setUnionId(appUser.getWxUnionId());
        request.getQueryCriteria().setUid(appUser.getUid());
        PaginationResponse<GroupTaskListResponse> response = groupTaskService.listEnded(
                request);
        return HttpResult.succ(response);
    }

    @PostMapping("/market/calender/list")
    @ApiOperation(value = "查询营销日历团长任务列表")
    public HttpResult<List<GroupTaskListResponse>> marketCalendarList(
            @RequestBody @Validated MarketTaskCalenderRequest request) {
        request.setUnionId(appUser.getWxUnionId());
        request.setUid(appUser.getUid());
        List<GroupTaskListResponse> response = groupTaskService.marketCalendarList(request);
        return HttpResult.succ(response);
    }

    @PostMapping("/commission/reward")
    @ApiOperation(value = "团长任务CPA预估奖励金")
    public HttpResult<TaskCommissionResponse> taskReward(@RequestBody @Validated TaskCommissionRequest request) {
        request.setUnionId(appUser.getWxUnionId());
        return groupTaskCoreClient.taskReward(request);
    }

    @PostMapping("/commission/rewardNew")
    @ApiOperation(value = "新版团长任务CPA预估奖励金")
    public HttpResult<TaskIncomeResponse> rewardNew(@RequestBody @Validated TaskIncomeRequest request) {
        request.setUid(appUser.getUid());
        return groupTaskCoreClient.taskRewardNew(request);
    }

    @PostMapping("/commission/pageRewardList")
    @ApiOperation(value = "分页查团长任务奖励详情")
    HttpResult<PaginationResponse<TaskRewardListResponse>> taskPageRewardList(
            @RequestBody @Validated PaginationRequest<TaskRewardListRequest> request) {
        if (Objects.isNull(request.getQueryCriteria())) {
            request.setQueryCriteria(new TaskRewardListRequest());
        }
        request.getQueryCriteria().setUnionId(appUser.getWxUnionId());
        return groupTaskCoreClient.pageRewardList(request);
    }

}
